#pragma once

#include "proxy/common/factory.h"
#include "config.h"


struct Client;
struct Router;

struct Tunnel {

  Tunnel (Client* client, const Config& config);

  void startup (Router* router);
  void shutdown ();

  void upLoop ();
  void downLoop ();

  void handshake ();
  void write (Ptr<IpPackage> pkg);


public:
  List joint;

private:
  const Config& _cfg;
  Client* _client;
  Router* _router;
  jlib::RSA _rsa;
  jlib::AES _aes;
  u8 _aes_key[32];

  s32 _socket {0};

  std::thread* _upWorker {nullptr};
  std::thread* _downWorker {nullptr};

  BlockQueue<IpPackage, &IpPackage::joint> _queue;
  std::mutex _cache_mutex;
  ByteBuffer<> _cache {IpPackage::MAX_SIZE + 16};
};
